package cn.caishen.report.controller;

import cn.caishen.report.common.dynamic.DynamicDataSourceService;
import cn.caishen.report.domain.report.ReportConfig;
import cn.caishen.report.service.report.ReportConfigService;
import cn.caishen.report.service.test.OrderService;
import cn.caishen.report.service.test.StorageService;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/order")
@Slf4j
public class OrderController {

    private final OrderService orderService;
    private final StorageService storageService;

    private final ReportConfigService reportConfigService;

    public OrderController(OrderService orderService, StorageService storageService, ReportConfigService reportConfigService) {
        this.orderService = orderService;
        this.storageService = storageService;
        this.reportConfigService = reportConfigService;
    }

    @PostMapping("/order")
    public String order() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(
                "jdbc:mysql://10.4.10.77:3306/report2?useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8");
        dataSource.setPassword("1234");
        dataSource.setUsername("root");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        DynamicDataSourceService.addDataSource("report", dataSource);
        DynamicDataSourceService.switchDb("report");
        List<ReportConfig> list = reportConfigService.list();
        return String.valueOf(list.size());
    }

    @PostMapping("/storage")
    public String storage() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(
                "jdbc:mysql://10.4.10.77:3306/dynamic_report?useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8");
        dataSource.setPassword("1234");
        dataSource.setUsername("root");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        DynamicDataSourceService.addDataSource("report", dataSource);
        DynamicDataSourceService.switchDb("report");
        List<ReportConfig> list = reportConfigService.list();
        return String.valueOf(list.size());
    }

}
